<html>
<head>
    <title>Quick add coupon</title>
    <style>
    #form{width: 800px;}
    input{width:100%;line-height: 30px;height: 35px;font-size: 20px;border: 1px solid #999;border-radius: 2px;}
    textarea{width:100%;height: 100px;}
    select{height: 30px;}
    hr{border:1px dotted pink}
</style>
</head>
<body>
<?php
	if (!in_array(cpx_get_user_role_name(), array('administrator','editor_ext','editor','editor_hong')))
	{
		die('You do not have permission to access this page!');
	}
	global $current_user;
	echo 'Welcome, ' . $current_user->user_login . ' (' . $current_user->roles[0] . ')';
    $home = home_url();
    echo "<a href='{$home}'> Home</a>";

    $args = array('hide_empty' => false);
    $tax = get_terms('event',$args);
	if ($_POST)
	{
        $events = $_POST['event'];
		$title = $_POST['form_title'];
		$desc = $_POST['form_desc'];
		$cp_type = $_POST['form_cp_type'];
		$cp_code = $_POST['form_code'];
        $exclusive = $_POST['form_exclusive'];
        $product_link = $_POST['form_product_link'];
		$expire_date = $_POST['form_expire'];
		$store_id = $_POST['form_store'];
		$status = 'publish';
		$author = $current_user->ID;

		$args = array(
			'post_title' => $title,
			'post_content' => $desc,
			'post_status' => $status,
			'post_author' => $author,
			'post_type' => 'coupon'
            );
		$post_id = wp_insert_post($args);
		if ($post_id > 0)
		{
			add_post_meta($post_id, 'coupon_type_metadata', $cp_type, true);
            if($product_link)
                add_post_meta($post_id, 'product_link_metadata', $product_link, true);
            if($exclusive > 0)
                add_post_meta($post_id, 'exclusive_metadata', $exclusive, true);
			if ($cp_code)
				add_post_meta($post_id, 'coupon_code_metadata', $cp_code, true);
			if ($expire_date)
				add_post_meta($post_id, 'expire_date_metadata', $expire_date, true);
			add_post_meta($post_id, 'active_metadata', 'active', true);
			add_post_meta($post_id, 'store_coupon_metadata', $store_id);

			add_post_meta($post_id, 'like_metadata', 1, true);
			add_post_meta($post_id, 'dislike_metadata', 0, true);
			add_post_meta($post_id, 'cp_rate_metadata', 100, true);
            if(count($events) > 0)
            {
                wp_set_object_terms($post_id,$events,'event');
            }

		}
	}
?>
<?php
	if ($_GET['st'])
	{
		$stid = $_GET['st'];
		global $wpdb;
		$qr = "select post_title from $wpdb->posts where ID = $stid";
		$rs = $wpdb->get_row($qr);
		if ($rs)
			$st_name = $rs->post_title;
	}
?>
<h1>New Coupon ( <a title="View store" target="_blank" href="<?php echo site_url('/?p='.$stid);?>"><?php echo $st_name; ?></a> )</h1>
<form action="" method="post">
<div id="form">
    <input type="text" name="form_title" placeholder="Title" /><hr />
    <textarea name="form_desc" placeholder="Description"></textarea><hr />
    Coupon type
    <select name="form_cp_type">
        <option value="code">Coupon Code</option>
        <option value="promotion">Promotion coupon</option>
        <option value="free_shipping">Free shipping coupon</option>
    </select>
    <hr />
    Exclusive
    <select name="form_exclusive">
        <option value="0" selected="selected">Default</option>
        <option value="1">Exclusive</option>
    </select>
    <hr />
    <input type="text" name="form_product_link" placeholder="Product link" /><hr />
    <input type="text" name="form_code" placeholder="Coupon code" /><hr />
    <input type="text" name="form_expire" placeholder="Expire date (yyyy/mm/dd)" /><hr />
    <!-- EVENTS -->
    Select event <br />
    <?php
    foreach ($tax as $k=>$v):
        if(strpos($v->name, '2013') == false):
    ?>
    <input style="width: auto;height: auto;" type="checkbox" name="event[]" value="<?php echo $v->name; ?>" ><?php echo $v->name; ?></input>
    <br />
    <?php
        endif;
    endforeach; ?>
    <hr />
    <!-- Store ID -->
    <input type="hidden" placeholder="Store ID" name="form_store" value='<?php echo $_GET['st']; ?>' />
    <input type="submit" class="submit" value="Publish" style="cursor: pointer;" />
    <?php
    	if ($post_id > 0):
    ?>
    <label style="background: greenyellow;">Success</label>
    <a target="_blank" title="Edit coupon" href="<?php echo admin_url('/post.php?post=' . $post_id . '&action=edit'); ?>"><?php echo $title; ?></a>
    <?php
    	endif;
    ?>
    </div>
</form>
<div></div>
</body>
</html>